<template>
  <div
    class="pagination-container"
    :class="{'inline': inline, 'position': position}"
  >
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :background="background"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script lang="ts">
// 非ts 版
import { scrollTo } from '@/utils/scroll-to'
import { defineComponent, PropType } from 'vue'
export default defineComponent({
  props: {
    total: {
      type: Number,
      default: 0
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 20
    },
    pageSizes: {
      type: Array as PropType<Array<number>>,
      default: () => [10, 20, 50, 200]
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    inline: {
      type: Boolean,
      default: false
    },
    position: {
      type: String,
      default: 'center'
    }
  },
  emits: ['update:page', 'pagination', 'update:limit'],
  computed: {
    currentPage: {
      get(): number {
        return this.page
      },
      set(value) {
        this.$emit('update:page', value)
      }
    },
    pageSize: {
      get(): number {
        return this.limit
      },
      set(value) {
        this.$emit('update:limit', value)
      }
    }
  },
  methods: {
    handleSizeChange(value) {
      this.$emit('pagination', { page: this.currentPage, limit: value })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleCurrentChange(value) {
      this.$emit('pagination', { page: value, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    }
  }
})
</script>

<style lang="less" scoped>
.el-pagination{
  white-space: normal;
}
.pagination-container {
  background: #fff;
  padding: 32px 16px;
  text-align: center;
}
.inline {
  display: inline-block;
}
.left {
  text-align: left;
}
.center {
  text-align: center;
}
.right {
  text-align: right;
}
</style>
